﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ylh.Data;
using Ylh.ElvModel;
using Ylh.Enums;
using System.Data;

namespace Ylh.ElvBLL
{
    public class DepartmentElvBLL
    {
        /// <summary>
        /// 插入一条数据,并且放回ID号码
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="model">TbDepartment对象</param>
        /// <returns></returns>
        public bool InsertDepartment(string ConStr, TbDepartment model)
        {
            return TbDepartment.InserObjedct(model, ConStr);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="model">TbDepartment对象</param>
        /// <returns></returns>
        public bool UpdateDepartment(string ConStr, TbDepartment model)
        {
            return TbDepartment.Update(model, ConStr);
        }


        /// <summary>
        /// 判断部门名称是否存在
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="DeparName">部门名称</param>
        /// <returns></returns>
        public bool IsDeParNameExists(string ConStr, string DeparName)
        {
            TbDepartment depart = new TbDepartment();
            OQLWhere where = new OQLWhere(depart);
            where.AND(depart.DepartmentName, OperateType.等于, DeparName);
            return TbDepartment.Select(where, ConStr).Rows.Count > 0;
        }

        /// <summary>
        /// 判断部门下面是否存在员工
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="DepartmentID">部门编号</param>
        /// <returns></returns>
        public bool IsdeparmentHseStaff(string ConStr, string DepartmentID)
        {
            TbDepartment depart = new TbDepartment();
            OQLWhere where = new OQLWhere(depart);
            where.AND(depart.DepartmentID, OperateType.等于, DepartmentID);
            return TbDepartment.Select(where, ConStr).Rows.Count > 0;
        }
        /// <summary>
        /// 根据部门编号删除部门信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="DepartmentID">部门编号</param>
        /// <returns></returns>
        public bool DeleteDepartmentInfo(string ConStr, string DepartmentID)
        {
            TbDepartment depart = new TbDepartment();
            OQLWhere where = new OQLWhere(depart);
            where.AND(depart.DepartmentID, OperateType.等于, DepartmentID);
            return TbDepartment.Delete(where, ConStr);
        }

        /// <summary>
        ///  方法重写
        ///  获取员工名称需要在[StaffElvBll中重新获取]
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="DepartmentID"></param>
        /// <returns></returns>
        public DataTable GetDepartMentInfo(string ConStr, decimal DepartmentID)
        {
            TbDepartment depart = new TbDepartment();
            OQLWhere where = new OQLWhere(depart);
            if (DepartmentID != -1)
                where.AND(depart.DepartmentID, OperateType.等于, DepartmentID);
            return TbDepartment.Select(where, ConStr);
            #region 原逻辑
            //            string sql = @"SELECT TbDepartment.* ,TbStaff.StaffName FROM TbDepartment 
            //            LEFT JOIN TbStaff on TbDepartment.StaffID=TbStaff.StaffID where 1=1";
            //            if (DepartmentID != -1)
            //            {
            //                sql += "  and  TbDepartment.DepartmentID=" + DepartmentID;
            //            }
            #endregion
        }


        /// <summary>
        /// 方法重写,没有获取员工名称
        /// 获取服务部门信息
        /// 两个条件同时作用
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="DepartmentID">部门ID,如果为-1则获取所有服务部门的信息</param>
        /// <param name="IsServer">
        /// true 获取服务部门
        /// false 获取非服务部门
        /// null 获取所有部门信息
        /// </param>
        /// <returns></returns>
        public DataTable GetServerDepartMentInfo(string ConStr, decimal DepartmentID, bool? IsServer)
        {
            #region 原先逻辑


            //            string sql = @"SELECT TbDepartment.* ,TbStaff.StaffName FROM TbDepartment
            //            LEFT JOIN TbStaff on TbDepartment.StaffID=TbStaff.StaffID where 1=1";

            //            if (IsServer != null)
            //            {
            //                if (Convert.ToBoolean(IsServer))
            //                {
            //                    sql += "  and  TbDepartment.IsServer<>0 ";
            //                }
            //                else
            //                {
            //                    sql += "  and  TbDepartment.IsServer=0 ";
            //                }

            //                if (DepartmentID != -1)
            //                {
            //                    sql += "  and  TbDepartment.DepartmentID=" + DepartmentID;
            //                }
            //            }
            //            else
            //            {
            //                if (DepartmentID != -1)
            //                {
            //                    sql += "  and  TbDepartment.DepartmentID=" + DepartmentID;
            //                }
            //            }

            #endregion
            TbDepartment depart = new TbDepartment();
            OQLWhere where = new OQLWhere(depart);

            if (DepartmentID != -1)
            {
                where.AND(depart.DepartmentID, OperateType.等于, DepartmentID);
            }

            if (IsServer != null)
            {
                if (Convert.ToBoolean(IsServer))
                    where.AND(depart.IsServer, OperateType.不等于, 0);
                else
                    where.AND(depart.IsServer, OperateType.等于, 0);
            }
            return TbDepartment.Select(where, ConStr);
        }

        /// <summary>
        /// 设置部门的负责人
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="staffID">负责人ID</param>
        /// <param name="DeparmentID">部门编号</param>
        /// <returns></returns>
        public bool SetDeparmentHead(string ConStr, string staffID, string DeparmentID)
        {
            TbDepartment depart = new TbDepartment();
            depart.StaffID = staffID;
            depart.DepartmentID = DeparmentID;
            return TbDepartment.Update(depart, ConStr);
        }
    }
}
